package com.atty.dms.controller;

import com.atty.dms.common.convention.result.Result;
import com.atty.dms.common.convention.result.Results;
import com.atty.dms.dao.entity.DormitoryDO;
import com.atty.dms.dto.req.DormitoryStudentCFReqDTO;
import com.atty.dms.dto.req.DormitoryStudentCreateReqDTO;
import com.atty.dms.dto.req.DormitoryStudentUpdateReqDTO;
import com.atty.dms.dto.resp.DormitoryStudentDetailPageRespDTO;
import com.atty.dms.dto.resp.DormitoryStudentPageRespDTO;
import com.atty.dms.dto.resp.DormitoryStudentRespDTO;
import com.atty.dms.service.DormitoryStudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class DormitoryStudentController {

    private final DormitoryStudentService dormitoryStudentService;

    // 手动创建宿舍学生分配记录
    @PostMapping("/api/dms/admin/assign")
    public Result<Void> assignDormitory(@RequestBody DormitoryStudentCreateReqDTO requestParam) {
        dormitoryStudentService.assignDormitoryStudent(requestParam);
        return Results.success();
    }

    // 根据宿舍id获取宿舍学生分配记录
    @GetMapping("/api/dms/admin/assign/{dormitoryId}")
    public Result<List<DormitoryStudentRespDTO>> getDormitoryStudentByDormitoryId(@PathVariable("dormitoryId") String dormitoryId) {
        Long l = Long.parseLong(dormitoryId);
        return Results.success(dormitoryStudentService.getDormitoryStudentByDormitoryId(l));
    }


    // 根据学生id获取宿舍学生分配记录
    @GetMapping("/api/dms/admin/assign/student/{studentId}")
    public Result<DormitoryStudentRespDTO> getDormitoryStudentByStudentId(@PathVariable("studentId") String studentId) {
        Long l = Long.parseLong(studentId);
        return Results.success(dormitoryStudentService.getDormitoryStudentByStudentId(l));
    }

    // 更改学生宿舍信息
    @PutMapping("/api/dms/admin/assign")
    public Result<Void> updateDormitoryStudent(@RequestBody DormitoryStudentUpdateReqDTO requestParam) {
        dormitoryStudentService.updateDormitoryStudent(requestParam);
        return Results.success();
    }

    // 删除宿舍学生分配记录
    @DeleteMapping("/api/dms/admin/assign/{studentId}")
    public Result<Void> deleteDormitoryStudent(@PathVariable("studentId") String studentId) {
        Long l = Long.parseLong(studentId);
        dormitoryStudentService.deleteDormitoryStudent(l);
        return Results.success();
    }
    //  交换宿舍
    @PostMapping("/api/dms/admin/assign/exchange")
    public Result<Void> exchangeDormitory(@RequestParam Long studentIdA, @RequestParam Long studentIdB ) {
        dormitoryStudentService.exchangeDormitoryStudent(studentIdA, studentIdB);
        return Results.success();
    }

    // 分页查询宿舍的全部分配记录
    @GetMapping("/api/dms/admin/assign/all")
    public Result<Page<DormitoryStudentDetailPageRespDTO>> getAllDormitoryStudentDetailByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int PageSize) {
        Page<DormitoryStudentDetailPageRespDTO> result = dormitoryStudentService.getAllDormitoryStudentDetailByPage(pageNum, PageSize);
        return Results.success(result);
    }

    /**
     * 单独分配宿舍
     */
    @PostMapping("/api/dms/admin/assign/cf")
    public Result<DormitoryDO> assignDormitoryByCollaborativeFiltering(@RequestBody DormitoryStudentCFReqDTO requestParam) {
        return Results.success(dormitoryStudentService.assignDormitoryByCollaborativeFiltering(requestParam));
    }

    /**
     * 批量分配宿舍
     */
    @PostMapping("/api/dms/admin/assign/cf/batch")
    public Result<Void> assignDormitoriesByCollaborativeFiltering(@RequestBody List<DormitoryStudentCFReqDTO> requestParams ){
        dormitoryStudentService.assignDormitoriesByCollaborativeFiltering(requestParams);
        return Results.success();
    }



}